package com.zgt.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zgt.dao.TeacherDao;
import com.zgt.model.Teacher;
import com.zgt.service.TeacherService;
import com.zgt.util.MD5;
import com.zgt.util.Page;

@Service("teacherService")
public class TeacherServiceImpl implements TeacherService {

	private TeacherDao teacherDao;
	private MD5 md5 = new MD5();

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	@Resource
	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public void deleteTeacherById(int id) {
		teacherDao.deleteTeacherById(id);
	}

	public Teacher getTeacherById(int id) {
		return teacherDao.getTeacherById(id);
	}

	public Page listTeacher(Page page) {
		page.setAllRow(teacherDao.getCount("from Teacher"));

		List teachers = teacherDao.listTeacher(page.getOffset(), page
				.getPageSize());
		page.setPageResult(teachers);

		return page;
	}

	public void addTeacher(Teacher teacher) {
		teacherDao.saveTeacher(teacher);
	}

	public void updateTeacher(Teacher teacher) {
		teacherDao.updateTeacher(teacher);
	}

	public void addTeachers(List temList) {
		List list = new ArrayList<Teacher>();
		Iterator li = temList.iterator();
		while (li.hasNext()) {
			Teacher tea = new Teacher();

			tea.setWorkNum((String) li.next());
			tea.setPassword(md5.getMD5ofStr((String) li.next()) );
			tea.setName((String) li.next());
			tea.setSex((String) li.next());
			tea.setTitle((String) li.next());
			tea.setEdu((String) li.next());
			tea.setDegree((String) li.next());
			tea.setStudentCount(Integer.parseInt((String) li.next()));

			if ((!tea.getWorkNum().isEmpty() && !tea.getPassword().isEmpty())
					&& (tea.getWorkNum() != "" && tea.getWorkNum() != null)
					&& (tea.getPassword() != "" && tea.getPassword() != null)) {
				list.add(tea);
			}

		}
		teacherDao.saveTeachers(list);
	}

	public Page listSearch(Page page, String key, String keyType) {
		String query = "from Teacher where " + keyType + " like '%" + key
				+ "%'";

		page.setAllRow(teacherDao.getCount(query.toString()));
		List teachers = teacherDao.listSearch(query.toString(), page
				.getOffset(), page.getPageSize());
		page.setPageResult(teachers);

		return page;
	}

	public List<Teacher> toExcle(String key, String keyType) {

		return teacherDao.toExcle(keyType, key);
	}

	public Teacher checkLogin(Teacher teacher) {
		return teacherDao.checkLogin(teacher);
	}

}
